package com.zenithmind.library.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 图书分类实体
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(name = "图书分类实体", description = "图书分类信息")
@TableName("lib_category")
public class Category extends BaseEntity {

    @NotBlank(message = "分类名称不能为空")
    @Size(max = 100, message = "分类名称长度不能超过100个字符")
    @Schema(description = "分类名称")
    private String name;

    @NotBlank(message = "分类编码不能为空")
    @Size(max = 50, message = "分类编码长度不能超过50个字符")
    @Schema(description = "分类编码")
    private String code;

    @Schema(description = "父分类ID")
    private String parentId;

    @Schema(description = "分类描述")
    @Size(max = 500, message = "分类描述长度不能超过500个字符")
    private String description;

    @Schema(description = "排序号")
    private Integer sortOrder;

    @Schema(description = "分类图标")
    private String icon;

    @Schema(description = "分类颜色")
    private String color;

    @Schema(description = "状态 (0-禁用, 1-启用)")
    private Integer status;

    @Schema(description = "是否叶子节点 (0-否, 1-是)")
    private Integer isLeaf;

    @Schema(description = "层级深度")
    private Integer level;

    @Schema(description = "分类路径")
    private String path;

    /**
     * 判断是否启用
     */
    public boolean isEnabled() {
        return status != null && status == 1;
    }

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "禁用";
            case 1 -> "启用";
            default -> "未知";
        };
    }

    /**
     * 判断是否为根分类
     */
    public boolean isRootCategory() {
        return parentId == null || parentId.trim().isEmpty();
    }

    /**
     * 判断是否为叶子节点
     */
    public boolean isLeafNode() {
        return isLeaf != null && isLeaf == 1;
    }

    /**
     * 获取层级深度（从1开始）
     */
    public int getDepth() {
        return level != null ? level : 1;
    }

    /**
     * 判断是否为顶级分类
     */
    public boolean isTopLevel() {
        return getDepth() == 1;
    }

    /**
     * 生成分类路径
     */
    public String generatePath(String parentPath) {
        if (parentPath == null || parentPath.trim().isEmpty()) {
            return code;
        }
        return parentPath + "/" + code;
    }

    /**
     * 获取父分类路径
     */
    public String getParentPath() {
        if (path == null || !path.contains("/")) {
            return null;
        }
        int lastSlash = path.lastIndexOf("/");
        return path.substring(0, lastSlash);
    }

    /**
     * 判断是否为指定分类的子分类
     */
    public boolean isChildOf(String parentCategoryId) {
        return parentId != null && parentId.equals(parentCategoryId);
    }

    /**
     * 判断是否为指定分类的后代分类
     */
    public boolean isDescendantOf(String ancestorPath) {
        return path != null && ancestorPath != null &&
               path.startsWith(ancestorPath + "/");
    }
}
